package com.example.xxcjgl.ui.components.common

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

@Composable
fun CaptchaView(
    captcha: String,
    onRefresh: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .background(
                color = getRandomLightColor(),
                shape = RoundedCornerShape(8.dp)
            )
            .clickable { onRefresh() }
            .padding(12.dp)
    ) {
        Text(
            text = captcha,
            style = MaterialTheme.typography.headlineMedium,
            color = getRandomDarkColor(),
            fontFamily = FontFamily.Monospace,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.align(Alignment.Center)
        )
    }
}

private fun getRandomLightColor(): Color {
    val colors = listOf(
        Color(0xFFFFE0E0), Color(0xFFE0FFE0), Color(0xFFE0E0FF),
        Color(0xFFFFF0E0), Color(0xFFE0FFFF), Color(0xFFFFE0FF)
    )
    return colors.random()
}

private fun getRandomDarkColor(): Color {
    val colors = listOf(
        Color(0xFFD32F2F), Color(0xFF388E3C), Color(0xFF1976D2),
        Color(0xFFF57C00), Color(0xFF00796B), Color(0xFF7B1FA2)
    )
    return colors.random()
}